package com.huodongrili.calender.util;

import java.util.List;

import com.huodongrili.calender.BuildConfig;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.provider.Settings.Secure;

/**
 * [22:06:32] Liang Sun: i@liangsun.org
[22:23:16] Andy Ma: xiaobao.andy@gmail.com
[22:27:51] Liang Sun: https://bitbucket.org/lsun/huodongrili/wiki/Home
 * @author Syleee
 *
 */

public class DeviceUtil {

	public static String getCurrentVersion(Context context) {
		String res = "0";
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			String version = info.versionName;
			/*if (version.contains(".")) {
				version = version.replaceAll(".", "");
			}*/
			int versioncode = info.versionCode;
			
			StringBuilder sb = new StringBuilder();
			sb.append(version);
			sb.append(".");
			sb.append(versioncode);
			
			res = sb.toString();
			
		} catch (NameNotFoundException e) {
			if (BuildConfig.DEBUG) {
				e.printStackTrace();
			}

		}
		return res;
	}

	public static String getAndroidId(Context c) {

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {

			return android.os.Build.SERIAL;

		} else {
			return Secure.getString(c.getContentResolver(), Secure.ANDROID_ID);
		}

	}
	
	public static boolean isServiceRunning(Context context, String srvName) {
		ActivityManager mgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningServiceInfo> list = mgr
				.getRunningServices(Integer.MAX_VALUE);
		for (int i = 0; i < list.size(); i++) {
			if (srvName.equals(list.get(i).service.getClassName())) {
				return true;
			}
		}
		return false;
	}

}
